package com.jackwoo.jerrymouse.classloader;

import com.jackwoo.jerrymouse.util.FileUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Author: StupidZhe
 * Date: 2019/11/6
 * Desc: 对于自身应用可见
 **/
public class WebAppClassLoader extends ClassLoader {

    public WebAppClassLoader() {
        super(WebAppClassLoader.class.getClassLoader());
        System.out.println(WebAppClassLoader.class.getClassLoader().getName());
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        byte[] bytes = new byte[0];
        try {
            bytes = new FileInputStream(new File(name)).readAllBytes();
        } catch (IOException e) {
            throw new ClassNotFoundException();
        }
        return loadBytes(bytes);
    }

    public Class loadBytes(byte[] classBytes) {
        return defineClass(null, classBytes, 0, classBytes.length);
    }
}
